/*  
 *  Copyright (c) 2008 Adrian Necimal <necimal@gmail.com>
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

if (typeof SongbirdScrobbler == "undefined") {
  var SongbirdScrobbler = {
    load: function() {
      // create the sbScrobbler
      this._scrobblerService = Components.classes["@necimal.com/scrobbler;1"]
                                 .getService().wrappedJSObject;
      this._scrobblerService.load();
      
      // listen for play/stop
      gPPS.addListener(this.PlayStopObserver);
      
      // listen for pause/resume
      this._pausedRemote = SB_NewDataRemote("faceplate.paused", null);
      this._pausedRemote.bindObserver(this.PauseResumeObserver, true);
    },
    
    unload: function() {
      gPPS.removeListener(this.PlayStopObserver);
      this._pausedRemote.unbind();
    }
  }
  
  SongbirdScrobbler.PlayStopObserver = {
    onBeforeTrackChange: function() { },
    
    onTrackChange: function() {
      // a track has started playing
      var mediaItem = gPPS.playingView.mediaList.getItemByGuid(gPPS.currentGUID);
      
      var artist = mediaItem.getProperty("http://songbirdnest.com/data/1.0#artistName");
      var track = mediaItem.getProperty("http://songbirdnest.com/data/1.0#trackName");
      var album = mediaItem.getProperty("http://songbirdnest.com/data/1.0#albumName");
      var url = gPPS.core.getMetadata("url");
      var length = mediaItem.getProperty("http://songbirdnest.com/data/1.0#duration");
      length = parseInt(length/1000000); // in seconds, please
      
      SongbirdScrobbler._scrobblerService.start(artist, track, album, length, url);
    },
    
    onStop: function() {
      // a track has been stopped
      SongbirdScrobbler._scrobblerService.stop();
    }
  }
  
  SongbirdScrobbler.PauseResumeObserver = {
    observe: function(subject, topic, data) {
      if (topic == "faceplate.paused") {
        if (data == 1) {
          // the track has paused
          SongbirdScrobbler._scrobblerService.pause();
        }
        else {
          // the track has resumed
          SongbirdScrobbler._scrobblerService.resume();
        }
      }
    }
  }
}

window.addEventListener("load", function(e) { SongbirdScrobbler.load() }, false);
window.addEventListener("unload", function(e) { SongbirdScrobbler.unload() }, false);
